import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _class, _class2;

import React from 'react';
import { Button, Form, Input, Popconfirm, Select, InputNumber } from 'antd';
import { connect } from 'dva';
import { Link } from 'dva/router';
import uuid from 'uuid/v4';
import { Bind } from 'lodash-decorators';
import EditTable from 'components/EditTable';
import Lov from 'components/Lov';
import Checkbox from 'components/Checkbox';
import { addItemToPagination, delItemToPagination, isTenantRoleLevel, tableScrollWidth } from 'utils/utils';
import intl from 'utils/intl';
import { enableRender, operatorRender } from 'utils/renderer';
import { CODE } from 'utils/regExp';
var SheetTable = (_dec = connect(function (_ref) {
  var template = _ref.template,
      loading = _ref.loading;
  return {
    template: template,
    queryLines: loading.effects['template/queryLines']
  };
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(SheetTable, _React$Component);

  var _super = _createSuper(SheetTable);

  function SheetTable() {
    _classCallCheck(this, SheetTable);

    return _super.apply(this, arguments);
  }

  _createClass(SheetTable, [{
    key: "handleCreateSheet",
    value: function handleCreateSheet() {
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          _this$props$template = _this$props.template,
          _this$props$template$ = _this$props$template.templateTargetList,
          templateTargetList = _this$props$template$ === void 0 ? [] : _this$props$template$,
          templateTargetPagination = _this$props$template.templateTargetPagination;
      dispatch({
        type: 'template/updateState',
        payload: {
          templateTargetList: [{
            _status: 'create',
            id: uuid(),
            sheetIndex: '',
            sheetName: '',
            datasourceCode: '',
            tableName: '',
            ruleScriptCode: '',
            enabledFlag: 1
          }].concat(_toConsumableArray(templateTargetList)),
          templateTargetPagination: addItemToPagination(templateTargetList.length, templateTargetPagination)
        }
      });
    }
    /**
     * handleDeleteSheet - 删除未保存的行，分页减少1
     * @param {object} record - 行数据
     */

  }, {
    key: "handleDeleteSheet",
    value: function handleDeleteSheet(record) {
      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          _this$props2$template = _this$props2.template,
          _this$props2$template2 = _this$props2$template.templateTargetList,
          templateTargetList = _this$props2$template2 === void 0 ? [] : _this$props2$template2,
          pagination = _this$props2$template.pagination;
      var newList = templateTargetList.filter(function (item) {
        return item.id !== record.id;
      });
      dispatch({
        type: 'template/updateState',
        payload: {
          templateTargetList: newList,
          pagination: delItemToPagination(templateTargetList.length, pagination)
        }
      });
    }
    /**
     * handleSheetEdit - 设置行数据的编辑状态
     * @param {object} record - 行数据
     * @param {boolean} flag - 编辑标识
     */

  }, {
    key: "handleSheetEdit",
    value: function handleSheetEdit(record, flag) {
      var _this$props3 = this.props,
          _this$props3$template = _this$props3.template.templateTargetList,
          templateTargetList = _this$props3$template === void 0 ? [] : _this$props3$template,
          dispatch = _this$props3.dispatch;
      var newList = templateTargetList.map(function (item) {
        if (record.id === item.id) {
          return _objectSpread(_objectSpread({}, item), {}, {
            _status: flag ? 'update' : ''
          });
        } else {
          return item;
        }
      });
      dispatch({
        type: 'template/updateState',
        payload: {
          templateTargetList: newList
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

      var _this$props4 = this.props,
          _this$props4$template = _this$props4.template,
          _this$props4$template2 = _this$props4$template.templateTargetList,
          templateTargetList = _this$props4$template2 === void 0 ? [] : _this$props4$template2,
          _this$props4$template3 = _this$props4$template.templateTargetPagination,
          templateTargetPagination = _this$props4$template3 === void 0 ? {} : _this$props4$template3,
          _this$props4$template4 = _this$props4$template.code,
          code = _this$props4$template4 === void 0 ? {} : _this$props4$template4,
          _this$props4$queryLin = _this$props4.queryLines,
          queryLines = _this$props4$queryLin === void 0 ? false : _this$props4$queryLin,
          detailId = _this$props4.detailId,
          templateType = _this$props4.templateType;
      var columns = [{
        title: intl.get('cimp.template.view.title.pageNumber').d('页序号'),
        width: 200,
        dataIndex: 'sheetIndexMeaning',
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('sheetIndex', {
              initialValue: "".concat(record.sheetIndex),
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cimp.template.view.title.pageNumber').d('页序号')
                })
              }]
            })( /*#__PURE__*/React.createElement(Select, {
              style: {
                width: 150
              }
            }, (code['CIMP.IMPORT_SHEET'] || []).map(function (n) {
              return /*#__PURE__*/React.createElement(Select.Option, {
                key: n.value,
                value: n.value
              }, n.meaning);
            }))));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cimp.template.view.title.pageName').d('页名称'),
        width: 200,
        dataIndex: 'sheetName',
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('sheetName', {
              initialValue: val,
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cimp.template.view.title.pageName').d('页名称')
                })
              }]
            })( /*#__PURE__*/React.createElement(Input, null)));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cimp.template.view.title.dataSource').d('数据源'),
        dataIndex: 'datasourceDesc',
        width: 200,
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('datasourceCode', {
              initialValue: record.datasourceCode,
              rules: [{
                required: templateType === 'S',
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cimp.template.view.title.dataSource').d('数据源')
                })
              }]
            })( /*#__PURE__*/React.createElement(Lov, {
              allowClear: false,
              textValue: record.datasourceDesc,
              code: isTenantRoleLevel() ? 'CPFM.DATASOURCE' : 'CPFM.SITE.DATASOURCE',
              queryParams: {
                enabledFlag: 1,
                dsPurposeCode: 'DI'
              }
            })));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cimp.template.view.title.startLine').d('导入起始行'),
        width: 200,
        dataIndex: 'startLine',
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('startLine', {
              initialValue: val === undefined ? 2 : val,
              rules: [{
                required: true,
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cimp.template.view.title.startLine').d('导入起始行')
                })
              }]
            })( /*#__PURE__*/React.createElement(InputNumber, {
              min: 2,
              step: 1
            })));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cimp.template.view.title.officialDataTableName').d('正式数据表名'),
        dataIndex: 'tableName',
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('tableName', {
              initialValue: val,
              rules: [{
                required: templateType === 'S',
                message: intl.get('cpaas.common.validation.notNull', {
                  name: intl.get('cimp.template.view.title.officialDataTableName').d('正式数据表名')
                })
              }, {
                pattern: CODE,
                message: intl.get('cpaas.common.validation.code').d('大小写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”')
              }]
            })( /*#__PURE__*/React.createElement(Input, {
              trim: true,
              inputChinese: false
            })));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get('cimp.template.view.title.validationRule').d('校验规则'),
        dataIndex: 'scriptDescription',
        width: 200,
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('ruleScriptCode', {
              initialValue: record.ruleScriptCode
            })( /*#__PURE__*/React.createElement(Lov, {
              allowClear: false,
              textValue: record.scriptDescription,
              code: "CIMP.IMPORT_SCRIPT",
              queryParams: {
                enabledFlag: 1
              }
            })));
          } else {
            return val;
          }
        }
      }, {
        title: intl.get("cpaas.common.status.enable").d('启用'),
        dataIndex: 'enabledFlag',
        width: 100,
        render: function render(val, record) {
          if (record._status === 'update' || record._status === 'create') {
            var getFieldDecorator = record.$form.getFieldDecorator;
            return /*#__PURE__*/React.createElement(Form.Item, null, getFieldDecorator('enabledFlag', {
              initialValue: record.enabledFlag
            })( /*#__PURE__*/React.createElement(Checkbox, null)));
          } else {
            return enableRender(val);
          }
        }
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 160,
        fixed: 'right',
        render: function render(val, record) {
          var operators = [];

          if (record._status === 'update') {
            operators.push({
              key: 'cancel',
              ele: /*#__PURE__*/React.createElement("a", {
                onClick: function onClick() {
                  return _this.handleSheetEdit(record, false);
                }
              }, intl.get('cpaas.common.button.cancel').d('取消')),
              len: 2,
              title: intl.get('cpaas.common.button.cancel').d('取消')
            });
          } else if (record._status === 'create') {
            operators.push({
              key: 'delete',
              ele: /*#__PURE__*/React.createElement(Popconfirm, {
                title: intl.get("cimp.template.view.message.title.confirmDelete").d('确定删除？'),
                onConfirm: function onConfirm() {
                  return _this.handleDeleteSheet(record);
                },
                style: {
                  textAlign: 'center'
                }
              }, /*#__PURE__*/React.createElement("a", null, intl.get('cpaas.common.button.delete').d('删除'))),
              len: 2,
              title: intl.get('cpaas.common.button.delete').d('删除')
            });
          } else {
            if (record.id !== undefined) {
              operators.push({
                key: 'delete',
                ele: /*#__PURE__*/React.createElement(Link, {
                  to: "/cimp/template/column/".concat(detailId, "/").concat(record.id, "/").concat(templateType)
                }, intl.get('cimp.template.model.template.editTemplateCol').d('维护模板列')),
                len: 5,
                title: intl.get('cimp.template.model.template.editTemplateCol').d('维护模板列')
              });
            }

            operators.push({
              key: 'edit',
              ele: /*#__PURE__*/React.createElement("a", {
                onClick: function onClick() {
                  return _this.handleSheetEdit(record, true);
                }
              }, intl.get('cpaas.common.button.edit').d('编辑')),
              len: 2,
              title: intl.get('cpaas.common.button.edit').d('编辑')
            });
          }

          return operatorRender(operators, record);
        }
      }];
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-operator"
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleCreateSheet,
        type: "primary"
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        rowKey: "id",
        loading: queryLines,
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        dataSource: templateTargetList,
        pagination: templateTargetPagination // FIXME: 这边没有 loadLines 方法, 也没有继承
        ,
        onChange: function onChange(page) {
          return _this.loadLines(page);
        }
      }));
    }
  }]);

  return SheetTable;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleCreateSheet", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCreateSheet"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteSheet", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteSheet"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSheetEdit", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSheetEdit"), _class2.prototype)), _class2)) || _class);
export { SheetTable as default };